package com.mostromas.ess.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mostromas.ess.entities.UserProjectAccount;
import com.mostromas.ess.project.mapper.UserProjectAccountMapper;
import com.mostromas.ess.project.req.UserProjectAccountREQ;
import com.mostromas.ess.project.service.IUserProjectAccountService;
import com.mostromas.ess.util.base.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class UserProjectAccountServiceImpl extends ServiceImpl<UserProjectAccountMapper, UserProjectAccount> implements IUserProjectAccountService {
    @Override
    public Result queryList(UserProjectAccountREQ req) {
        QueryWrapper<UserProjectAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("username",req.getUsername());
        wrapper.eq("project_id",req.getProjectId());
        return Result.ok(baseMapper.selectList(wrapper));
    }

    @Override
    public Result getInfoByName(UserProjectAccountREQ req) {
        System.out.println(req.getUsername());
        System.out.println(req.getProjectId());
        QueryWrapper<UserProjectAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("name",req.getUsername());
        wrapper.eq("project_id",req.getProjectId());
        return Result.ok(baseMapper.selectOne(wrapper));
    }

    @Override
    public Result getInfoByAccountName(UserProjectAccountREQ req) {
        QueryWrapper<UserProjectAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("username",req.getUsername());
        wrapper.eq("project_id",req.getProjectId());
        return Result.ok(baseMapper.selectOne(wrapper));
    }

    @Override
    public Result saveOrUpdateAccount(UserProjectAccount account) {
        QueryWrapper<UserProjectAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("name",account.getName());
        wrapper.eq("project_id",account.getProjectId());
        wrapper.eq("username",account.getUsername());
        if(StringUtils.isNotEmpty(account.getId()) && account.getId() != "null"){
            baseMapper.updateById(account);
        }else{
            super.saveOrUpdate(account,wrapper);
        }
        return Result.ok();
    }
}
